<template>
  <div class="message">
    <page-header title="消息管理">
      <page-search :config="searchConfig" :model="searchModel" @search="getData">
        <el-button @click="showDialog()">发布消息</el-button>
      </page-search>
    </page-header>
    <el-table :data="tableData" :max-height="clientHeight - 250">
      <el-table-column prop="title" label="标题" />
      <el-table-column prop="createTime" label="发布时间" />
      <el-table-column
        label="操作"
        min-width="120px"
      >
        <template slot-scope="{ row }">
          <action-text @click="showDialog(row)">修改</action-text>
          <action-text @click="deleteData(row)" type="danger">删除</action-text>
        </template>
      </el-table-column>
    </el-table>
    <page-pagination
      :pageSize="pageSize"
      :currentPage="pageNum"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <formDialog ref="dialog" :initFormData="formData" :isCreate="isCreate" @confirm="getData" />
  </div>
</template>
<script>
import paginationMixin from '@/mixins/pagination';
import { getMessageList, deleteMessage } from '@/api/base-config';
import formDialog from './dialog';

const initFormData = {
  title: '',
  content: '',
};

export default {
  data() {
    return {
      searchConfig: [
        {
          type: 'input',
          label: '标题',
          prop: 'title',
        },
        {
          type: 'daterange',
          label: '发布时间',
          prop: 'releaseAt',
        },
      ],
      searchModel: {
        userName: '',
        releaseAt: '',
      },
      tableData: [],
      formData: {
        ...initFormData,
      },
      isCreate: false,
    };
  },
  components: {
    formDialog,
  },
  mixins: [paginationMixin],
  methods: {
    async deleteData(row) {
      this.$confirm('确定删除', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(async () => {
        await deleteMessage({
          messageId: row.id,
        });
        this.$message.success('删除成功');
        await this.getData();
      }).catch(() => {});
    },
    showDialog(data) {
      if (data) {
        this.formData = { ...data };
        this.isCreate = false;
      } else {
        this.formData = { ...initFormData };
        this.isCreate = true;
      }
      this.$refs.dialog.dialogVisible = true;
    },
    async getData() {
      const {
        pageSize, pageNum, title, releaseAt,
      } = this.searchParams;
      let startTime;
      let endTime;
      if (releaseAt) {
        [startTime, endTime] = releaseAt;
      }
      const params = {
        pageSize,
        pageNum,
        title,
        startTime,
        endTime,
      };
      const result = await getMessageList(params);
      const { list, total } = result;
      this.total = total;
      this.tableData = list && list.map((item) => {
        const { createTime } = item;
        return {
          ...item,
          createTime: this.$formatTime(createTime),
        };
      });
    },
  },
  async created() {
    await this.getData();
  },
};
</script>
